package org.solrcn.hadoop.index;

import java.net.MalformedURLException;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.zookeeper.KeeperException;

import com.google.common.collect.Lists;
import com.google.common.hash.Hashing;

public class SolrLoadBlance {
	static CloudSolrServer server;
	static String zkHost = "localhost:2181";
	static List<String> nodes = Lists.newArrayList();

	static String getNode(List<String> nodes, String someId) {
		int bucket = Hashing.consistentHash(Hashing.murmur3_32().hashString(someId), nodes.size());

		return nodes.get(bucket);

	}

	public static void main(String[] args) {
		try {
			server = new CloudSolrServer(zkHost);
			server.setDefaultCollection("collection1");
			
			ModifiableSolrParams modifiableSolrParams = new ModifiableSolrParams();
//			modifiableSolrParams.add("collections","collection2");
			modifiableSolrParams.add("q","*:*");
			modifiableSolrParams.add("shards","shard2");
			QueryResponse query = server.query(modifiableSolrParams);
			SolrDocumentList results = query.getResults();
			for (int i = 0, n = results.size(); i < n; i++) {
				SolrDocument solrDocument = results.get(i);
				Object fieldValue = solrDocument.getFieldValue("id");
				Object fieldValue2 = solrDocument.getFieldValue("score");
			}
			
			
			
			long numFound = results.getNumFound();
			System.out.println(numFound);
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		server.connect();
		if (!server.getZkStateReader().getZkClient().isConnected()
				|| server.getZkStateReader().getZkClient().isClosed())
			server.connect();
		ClusterState clusterState = server.getZkStateReader().getClusterState();
		Set<String> liveNodes = clusterState.getLiveNodes();
		
		if(liveNodes.size()>0){
			
			
			
			for (String liveNode : liveNodes) {
				String baseUrlForNodeName = server.getZkStateReader().getZkClient().getBaseUrlForNodeName(liveNode);
				System.out.println("liveNode"+liveNode);
				System.out.println("baseUrlForNodeName:"+baseUrlForNodeName);
			}
			System.out.println("liveNodesSize:"+liveNodes.size());
			return;
		}
		
		
		Set<String> collections = clusterState.getCollections();
		for (String collection : collections) {
			Collection<Slice> slices = clusterState.getSlices(collection);
			System.out.println(collection);
			for (Slice slice : slices) {
				String sliceName = slice.getName();
				System.out.println("sliceName:" + sliceName);
				String leaderUrl;
				try {
					leaderUrl = server.getZkStateReader().getLeaderUrl(collection, sliceName, 1000);
					nodes.add(leaderUrl);
					System.out.println("leaderUrl:" + leaderUrl);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (KeeperException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Replica leader = clusterState.getLeader(collection, sliceName);
				if (leader != null) {
					String leaderName = leader.getName();
					String nodeName = leader.getNodeName();
					String shardId = clusterState.getShardId(leaderName);
					System.out.format("leaderName:%s,nodeName:%s,shardId:%s\n",
							leaderName, nodeName, shardId);
				} else {
					System.out.println("leader null");
				}
			}

		}

	}
}
